<?php
/**
* Name:  My Model
*
* Author: Ray Nowell
*
* Created:  10/26/2010
*
* Description:  Extends CI's Model 
*
*/
 
class MY_Model extends Model
{
	//REN TODO:  Use this vars in child classes to prevent the large amount of query class used to get this vars
	protected $is_admin = false;
	protected $facility_id = 0;
	protected $current_user = 'not set';
                    
	function MY_Model ($must_be_admin = false)  
	{
		parent::Model();
		$user	=	$this->ion_auth->get_user();
		
		$this->current_user	=	$user->username;
		$this->facility_id	=	$user->current_facility_id;
	}
	
	//Need to check foreign keys to make sure  
	//records can be deleted.  Return error message of each record
	//That cannot be deleted
	function delete_record($table_name, $table_id, $facility_filter = true)
	{
		//$this->output->enable_profiler(TRUE);
		$segment_array = $this->uri->segment_array();
		//Get rid of the first couple of segments as they are not needed for the IN clause
		unset($segment_array[1]);
		unset($segment_array[2]);
		
		//Check the remaining segments to see if they are in use
		foreach ($segment_array as $segment)
		{
			$fk_table_array	=	$this->_check_foreign_keys($table_name, $segment);
			if (count($fk_table_array) > 0)
			{
				foreach ($fk_table_array as $fk_table)
					$delete_errors[$fk_table]	=	!empty($delete_errors[$fk_table]) ? intval($delete_errors[$fk_table]) + 1 : 1; //Initialize if empty array
			}
			else
				$deletable_segment_array[]	=	$segment;
		}
		
		if (!empty($delete_errors))
		{
			//Return user a list of how many records could not be deleted based on the 4delete_errors count
			foreach ($delete_errors as $key => $value)
			{
				//Left off here
				if ($value == 1)
				{
					$record_text = 'record';
					$other_text	=	'it is';
				}
				else
				{
					$record_text = 'records';
					$other_text	=	'they are';
				}
				
				
				$ret_val[]	=	'Unable to delete '.$value.' '.$record_text.' because '.$other_text.' currently being used in '.$key.'.';
			}
		}
		
		if (!isset($deletable_segment_array) || empty($deletable_segment_array))		
			$ret_val[]	=	'Number of records deleted:  0';
		else
		{
			$this->db->where_in($table_id, $deletable_segment_array);
			//Filter the delete statement by facility (if applicable)
			if ($facility_filter)
				$this->db->where('facility_id',$this->facility_id);
			
			if ($this->db->delete($table_name))
				$ret_val[]	=	'Number of records deleted:  '.strval(count($deletable_segment_array));
			else
				$ret_val[]	=	'Problem with mass delete';
		}
		
		
		return $ret_val;
	}
	
	//Checks to see if the foreign key is in use
	function _check_foreign_keys($table_name, $table_id)
	{
		$this->load->helper('inflector');
		
		$dependency_table_array	=	null;
		$ret_val	=	null;

		switch ($table_name) 
		{
			case 'drug_list':
				$dependency_table_array	=	array('clinical_intervention');
				break;
			case 'facility':
				$dependency_table_array	=	array('clinical_intervention','patient','pharmacist','physician','physician_group','unit_patient_location');
				break;
			case 'patient':
				$dependency_table_array	=	array('clinical_intervention');
				break;
			case 'pharmacist':
				$dependency_table_array	=	array('clinical_intervention');
				break;
			case 'physician':
				$dependency_table_array	=	array('clinical_intervention');
				break;
			case 'physician_group':
				$dependency_table_array	=	array('physician');
				break;
			case 'unit_patient_location':
				$dependency_table_array	=	array('clinical_intervention');
				break;
			case 'clinical_intervention':
				$dependency_table_array	=	array(); //Clinical Interventions do not have any dependencies
				break;
		}
		
		foreach ($dependency_table_array as $dependency_table)
		{	
			$fk_field	=	$table_name === 'clinical_intervention'	?	'intervention_id'	:	$table_name.'_id';
			$this->load->model('Generic_model');
			$return_count	=	$this->_get_foreign_key_count($dependency_table, $fk_field, $table_id);
			if ($return_count	>	0)
				$ret_val[]	=	humanize(plural($dependency_table)); //Add the table to the returned array
		}
		
		return $ret_val;
	}
	
	function _get_foreign_key_count($table_name, $foreign_key_name, $id)
	{
		//$this->db->flush_cache();
		$this->db->where($foreign_key_name, $id);
		$this->db->from($table_name);
		return $this->db->count_all_results();
	}

} 
/* End of MY_Model.php */
/* Location: ./system/application/libraries/MY_code.php */

